package com.dfec.water.dashboard.controller;

import com.dfec.common.enums.BusinessType;
import com.dfec.common.exception.ServiceException;
import com.dfec.common.utils.AjaxResult;
import com.dfec.framework.operatelog.annotation.SysLog;
import com.dfec.water.config.PathProperties;
import com.dfec.water.entity.YdbForecastEntity;
import com.dfec.water.entity.YdbForecastListEntity;
import com.dfec.water.entity.vo.CalcGreenVO;
import com.dfec.water.service.YdbForecastListService;
import com.dfec.water.service.YdbForecastService;
import com.dfec.water.util.Base64ToImageConverter;
import com.dfec.water.util.GreenPixelCalculator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 大屏-预报预测
 *
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-02-2025/2/26 11:23:37
 */
@Tag(name = "大屏-预报预测")
@RestController
@RequestMapping("dashboard/forecast")
public class ForecastController {


    @Resource
    private PathProperties pathProperties;

    @Resource
    private YdbForecastService ydbForecastService;


    @Resource
    private  YdbForecastListService ydbForecastListService;



    /**
     * 绿色区域计算
     */
    @RequestMapping("/getGreenPer")
    @Operation(summary = "绿色区域计算")
    public AjaxResult<Long> getGreenPer(@RequestBody CalcGreenVO calcGreenVo) {
        String images = calcGreenVo.getImages();
        if (!StringUtils.isNotBlank(images)) {
            throw new ServiceException("图片不能为空");
        }

        String outPath = pathProperties.getFilePath() + "/image/";
        File file = new File(outPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        outPath = outPath + UUID.randomUUID() + ".png";
        try {
            Base64ToImageConverter.convertBase64ToImage(images, outPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        double greenPercentage = 0d;
        try {
            greenPercentage = GreenPixelCalculator.calculateGreenPercentage(outPath);
            System.out.println("绿色像素占比: " + greenPercentage + "%");
        } catch (IOException e) {
            e.printStackTrace();
        }
        DecimalFormat df = new DecimalFormat("#.00");
        if(greenPercentage >0){
            return AjaxResult.ok().put(df.format(greenPercentage));
        }else {
            return AjaxResult.ok().put(greenPercentage);
        }
    }



    /**
     * 获取预测数据-带子节点的
     */
    @GetMapping("/getForecastData")
    @Operation(summary = "获取预测数据")
    public AjaxResult<List<YdbForecastEntity>> getForecastData() {
        return AjaxResult.ok().put(ydbForecastService.getForecastData());
    }



    /**
     * 获取预测数据的详情列表
     */
    @GetMapping("/getForecastDetailList")
    @Operation(summary = "获取预测数据的详情列表")
    public AjaxResult getForecastDetailList(@RequestParam("forecastId") Long forecastId) {
        return AjaxResult.ok().put(ydbForecastService.getForecastDetailList(forecastId));
    }




    /**
     * 保存
     */
    @SysLog(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @Operation(summary = "新增")
    public AjaxResult save(@RequestBody YdbForecastListEntity ydbForecast) {
        ydbForecastListService.save(ydbForecast);
        return AjaxResult.ok();
    }

    /**
     * 修改
     */
    @SysLog(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @Operation(summary = "修改")
    public AjaxResult update(@RequestBody YdbForecastListEntity ydbForecast) {
        ydbForecastListService.updateById(ydbForecast);
        return AjaxResult.ok();
    }

    /**
     * 删除
     */
    @SysLog(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @Operation(summary = "删除")
    public AjaxResult delete(@RequestBody Long[] ids) {
        ydbForecastListService.removeByIds(Arrays.asList(ids));
        return AjaxResult.ok();
    }

}
